home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / mndlbrot.zip / MNDLBR1.PAS < prev   
Pascal/Delphi Source File  |  1988-07-05  |  10KB  |  285 lines

  1. {                        MANDELBROT
  2. [Downloaded from Delaware OPUS 17 Feb 87 as MNDLBR.PAS.
  3.  Tweaked for speed.
  4.  David Kirschbaum
  5.  Toad Hall
  6.  kirsch@braggvax.ARPA
  7. ]
  8. This program generates and displays Mandelbrots.  A Mandelbrot is a
  9. graphic representation of the mandelbrot set and the fleeing points
  10. around the set on the REAL/IMAGINARY plane.  Mathmatically, a point
  11. in the set is defined as a point which when iterated in the following
  12. manner will remain finite after an infinite number of iterations:
  13.  
  14.               1. c := point; z := 0; n := 0;
  15.               2. z := z*z + c;
  16.               3  n := n+1;
  17.               4. repeat until either z>2 or n is some large number
  18.  
  19. Obviously the iteration cannot be carried out to infinity so we set an
  20. upper limit to 255.  Thus "n" can just fit in one byte.  Typically large
  21. computers will only carry n to 1000, and there is very little difference
  22. between 255 and 1000 iterations.
  23.  
  24. The Mandelbrot set representation is a breathtakingly beautiful thing.
  25. You are encouraged to try and find an issue of August 1985 Scientific American
  26. for some really fantastic photos, as well as a well written article.
  27.  
  28. To operate the program just answer the questions.  A "C" will allow you
  29. to generate a mandelbrot and a "D" will allow you to display it with different
  30. "Breakpoints".  The IBM can only display 4 colors and 255 is defined as black.
  31.  
  32. You must enter 2 breakpoints: a lower and an upper.
  33. When n is between 0 and the lower breakpoint, color 1 will be displayed;
  34.           between breakpoint 1 and 2,         color 2 will be displayed;
  35.           and when between 2 and 255,         the third color is displayed.
  36.  
  37. Generating a file will usually require from 6 to  12 hours, or if an 8087
  38. chip is used (and Turbo 8087 is used for compiling)  the time is cut to
  39. "only" 2 to 4 hours.
  40.  
  41. It is recommended that the full Mandelbrot be computed first
  42. (RL,RU,IL,IU = -2,.5,-1.25,1.25), then blowups done from it.
  43. Remember to enter a carriage return after each number.
  44.  
  45. A disk for the IBM and compatibles which has this program and about 6 of the
  46. really good plots on it is available for $5 to cover the cost of the disk and
  47. shipping.
  48.  
  49. A disk with an advanced version of this program which allows windowing
  50. of an area in the display, so referencing is done automatically to
  51. the generate portion for an easy magnification of a specific area,
  52. is available for $15.
  53.  
  54. The advanced version will have standard as well as 8087 com files
  55. and includes many more features, as well as color pictures of several
  56. Mandelbrots and updates when new features are added.
  57.  
  58. To order or report bugs Reply to:   Marshall Dudley      or Compuserve
  59.                                     12402 W. Kingsgate Dr.  #72416,3357
  60.                                     Knoxville, Tn. 37922
  61.  
  62. This program may be duplicated and given away free provided
  63. this introduction is left untouched.
  64.  
  65. Modifications:  You may wish to try some modifications to this program.
  66. If this program is modified please indicate who and what mods were done below.
  67. I would be interested in hearing about any good mods and can be reached as
  68. above.
  69.  
  70. Please do not change the file structure.  It was done in this manner so that
  71. a file can be created and displayed by standard or 8087 turbo interchangeably.
  72. A change will cause compatibility problems.
  73.  
  74. }
  75.  
  76. PROGRAM Mandelbrot;
  77. {$U-} {no user interrupts for speed}
  78. {$C-} {no Ctrl C either}
  79. {$V-} {relax string type checking}
  80.  
  81. TYPE
  82.   Str23 = STRING[23];      {TH}
  83.  
  84.   Chunk = RECORD
  85.             Val1 : Str23;
  86.             Val2 : Str23;
  87.             Val3 : Str23;
  88.             Val4 : Str23;
  89.             littlechunk : ARRAY[0..319, 0..199] OF Byte;
  90.           END;
  91.  
  92. CONST
  93.   BEEP : CHAR = ^G;
  94.  
  95. VAR
  96.   breakPoint1,                 {Toad Hall: make these global for speed}
  97.   breakPoint2,
  98.   xPic,yPic,color : INTEGER;
  99.   realPart,imaginary,          {Toad Hall: make these global for speed}
  100.   zr,zi,                       {ditto}
  101.   stepX,stepY,                 {ditto}
  102.   zrSquared,ziSquared,         {ditto}
  103.   realUpper,realLower,
  104.   imagUpper,imagLower : REAL;
  105.   TName,
  106.   Name        : STRING[20];
  107.   n           : Byte;
  108.   ChunkFile   : FILE OF Chunk;
  109.   ChunkRec    : Chunk;
  110.   C,Choice    : CHAR;
  111.  
  112.  
  113. PROCEDURE Generate;
  114. (* Toad Hall: made these global
  115.   VAR
  116.     realPart,imaginary,
  117.     zr, zi,
  118.     stepX,stepY,
  119.     zrSquared,ziSquared : REAL;
  120. *)
  121.   BEGIN
  122.     WRITELN('Enter Lower and upper limits of Real & Imaginary parts');
  123.     WRITELN('as: RL RU IL IU.  Separate each with a space, end with a RETURN.');
  124. (*
  125.     READLN(realLower);
  126.     READLN(realUpper);
  127.     READLN(imagLower);
  128.     READLN(imagUpper);
  129. *)
  130.     Read(realLower, realUpper, imagLower, imagUpper);
  131.     Writeln;
  132.     WRITELN('Enter filename:');
  133.     Name := '';               {insure cleared in case null entry}
  134.     READLN(Name);
  135.     IF Name = '' THEN HALT;
  136.     GraphColorMode;
  137.     stepX := (realUpper-realLower)/320.0;
  138.     stepY := (imagUpper-imagLower)/200.0;
  139.     FOR xPic := 0 TO 319 DO BEGIN
  140.       FOR yPic := 0 TO 199 DO BEGIN
  141.         n := 0;
  142.         zr := 0.0;
  143.         zi := 0.0;
  144.         Plot(PRED(xPic), PRED(yPic), 3);
  145.         realPart := realLower  + INT(xPic) * stepX;
  146.         imaginary := imagLower + INT(yPic) * stepY;
  147.         zrSquared := 0.0;
  148.         ziSquared := 0.0;
  149.         REPEAT
  150.           zi := zi * zr * 2.0 + imaginary;
  151.           zr := zrSquared + realPart - ziSquared;
  152.           n := SUCC(n);
  153.           zrSquared := SQR(zr);
  154.           ziSquared := SQR(zi);
  155.         UNTIL ((zrSquared + ziSquared) > 4.0) OR (n > 254);
  156.         color := 3 - (n ShR 6);  {make 0 to 255 into 15 to 0 for graphing}
  157.         Plot(PRED(xPic), PRED(yPic), color);
  158.         ChunkRec.LittleChunk[xPic,yPic] := n;
  159.       END;
  160.       IF KeyPressed THEN BEGIN
  161.         READ(Kbd,C);
  162.         IF C = #3 THEN HALT;
  163.       END;
  164.     END;
  165.     TextMode;
  166.     WRITE(BEEP);                         {Beep at finish}
  167.     STR(realLower:23, ChunkRec.Val1);
  168.     STR(realUpper:23, ChunkRec.Val2);
  169.     STR(imagLower:23, ChunkRec.Val3);
  170.     STR(imagUpper:23, ChunkRec.Val4);
  171.     Assign(ChunkFile,Name);
  172.     REWRITE(ChunkFile);
  173.     WRITE(ChunkFile,ChunkRec);
  174.     CLOSE(ChunkFile);
  175.     WRITE(BEEP);
  176.   END;  {of Generate}
  177.  
  178.  
  179. PROCEDURE Print;
  180.   VAR
  181.     realUpper,
  182.     realLower,
  183.     imagUpper,
  184.     imagLower  : REAL;
  185. {    n          : Byte; Toad Hall: use global n }
  186.     Z          : STRING[10];
  187. {    breakPoint1, Toad Hall: made these global
  188.     breakPoint2,
  189. }
  190.     palet : INTEGER;
  191.     Rerun : BOOLEAN;                   {Toad Hall}
  192.  
  193.   FUNCTION value(numstring : Str23) : REAL;
  194.     VAR
  195.       temporary : REAL;
  196.       error : INTEGER;  {Toad Hall : moved from Print}
  197.     BEGIN
  198.       IF Numstring[21] = '0'           {if written by 8087 version}
  199.       THEN DELETE(Numstring,21,1);
  200.       REPEAT
  201.         DELETE(Numstring,1,1);
  202.       UNTIL ORD(NumString[1]) <> 32;   {delete spaces}
  203.       VAL(NumString,temporary,error);
  204.       value := temporary;
  205.     END;  {of value}
  206.  
  207.   BEGIN  {Print}
  208.     WRITELN('Enter Filename for data');
  209.     READLN(TName);
  210.     IF TName <> '' THEN BEGIN
  211.       Name := TName;
  212.       Rerun := FALSE;                  {Toad Hall: New file, so not a loop}
  213.     END;
  214.     IF Name = '' THEN Halt;
  215.  
  216.     IF NOT Rerun THEN BEGIN          {Toad Hall}
  217.      Assign(ChunkFile,Name);
  218.       RESET(ChunkFile);
  219.       READ(ChunkFile,ChunkRec);
  220.       CLOSE(ChunkFile);
  221.       realLower := value(ChunkRec.Val1);
  222.       realUpper := value(ChunkRec.Val2);
  223.       imagLower := value(ChunkRec.Val3);
  224.       imagUpper := value(ChunkRec.Val4);
  225.     END;
  226.  
  227.     REPEAT
  228.       IF Rerun THEN Read(breakPoint1, breakPoint2)
  229.       ELSE BEGIN
  230.         WRITELN('Real Boundries are:  ',realLower:10:8,'  ',realUpper:10:8);
  231.         WRITELN('Imaginary Boundries: ',imagLower:10:8,'  ',imagUpper:10:8);
  232.         WRITELN('255 will be black, Enter breakpoints for other two shades');
  233.         Writeln(' Format: <RETURN> for no change, nnn nnn<RETURN> for new entry:  ');
  234. (*
  235.     READLN(breakPoint1);
  236.     READLN(breakPoint2);
  237. *)
  238.         Read(breakPoint1, breakPoint2);  {TH}
  239.